package com.example.test.chapter14;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.example.test.R;

public class NoteEdit extends Activity {
	private EditText mTitle;
	private EditText mBody;
	private Long idNote;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.note_edit);
		mTitle = (EditText) findViewById(R.id.title);
		mBody = (EditText) findViewById(R.id.content);
		Button confrim = (Button) findViewById(R.id.confirm);
		idNote = null;
		Bundle extra = getIntent().getExtras();
		if (extra != null) {
			String title = extra.getString(NotesDbAdapter.KEY_TITLE);
			String body = extra.getString(NotesDbAdapter.KEY_BODY);
			idNote = extra.getLong(NotesDbAdapter.KEY_ROWID);
			if (title != null) {
				mTitle.setText(title);
			}
			if (body != null) {
				mBody.setText(body);
			}
		}
		confrim.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Bundle bundle = new Bundle();
				bundle.putString(NotesDbAdapter.KEY_TITLE, mTitle.getText()
						.toString());
				bundle.putString(NotesDbAdapter.KEY_BODY, mBody.getText()
						.toString());
				if (idNote != null) {
					bundle.putLong(NotesDbAdapter.KEY_ROWID, idNote);
				}
				Intent intent = new Intent();
				intent.putExtras(bundle);
				setResult(RESULT_OK, intent);
				finish();
			}
		});
	}
}
